package swb.api.com.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import swb.api.com.repository.SwOrderInfoRepository;
import swb.api.com.repository.dto.SwOrderInfo;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 订单回调处理控制器，用于接收并处理来自外部系统的订单回调请求。
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
@Api(tags = "订单管理接口")
public class SwOrderController {

    @Autowired
    private SwOrderInfoRepository orderRepo;

    /**
     * 处理订单回调请求，将订单信息保存到数据库中。
     * <p>
     * 该方法会首先检查订单是否已存在，如果已存在则直接返回成功响应；
     * 如果不存在，则创建新的订单对象并保存到数据库中。
     *
     * @param adid     广告ID
     * @param adname   广告名称
     * @param appid    应用ID
     * @param ordernum 订单编号
     * @param dlevel   设备等级
     * @param pagename 页面名称
     * @param atype    激活类型
     * @param deviceid 设备ID
     * @param simid    SIM卡ID
     * @param appsign  应用签名
     * @param merid    商户ID
     * @param event    事件类型
     * @param adicon   广告图标
     * @param price    单价
     * @param money    金额
     * @param itime    订单时间（格式：yyyy/MM/dd HH:mm:ss）
     * @param keycode  密钥码
     * @return 响应结果，包含success状态和message信息
     */
    @GetMapping("/callback")
    @ApiOperation(value = "处理订单回调", notes = "接收并处理来自外部系统的订单回调请求，保存订单信息到数据库")
    public Map<String, Object> handleCallback(
            @ApiParam(value = "广告ID", required = true) @RequestParam Integer adid,
            @ApiParam(value = "广告名称", required = true) @RequestParam String adname,
            @ApiParam(value = "应用ID", required = true) @RequestParam String appid,
            @ApiParam(value = "订单编号", required = true) @RequestParam String ordernum,
            @ApiParam(value = "设备等级", required = true) @RequestParam Integer dlevel,
            @ApiParam(value = "页面名称", required = true) @RequestParam String pagename,
            @ApiParam(value = "激活类型", required = true) @RequestParam Integer atype,
            @ApiParam(value = "设备ID", required = true) @RequestParam String deviceid,
            @ApiParam(value = "SIM卡ID", required = true) @RequestParam String simid,
            @ApiParam(value = "应用签名", required = true) @RequestParam String appsign,
            @ApiParam(value = "商户ID", required = true) @RequestParam String merid,
            @ApiParam(value = "事件类型", required = true) @RequestParam String event,
            @ApiParam(value = "广告图标", required = true) @RequestParam String adicon,
            @ApiParam(value = "单价", required = true) @RequestParam String price,
            @ApiParam(value = "金额", required = true) @RequestParam String money,
            @ApiParam(value = "订单时间（格式：yyyy/MM/dd HH:mm:ss）", required = true) @RequestParam String itime,
            @ApiParam(value = "密钥码", required = true) @RequestParam String keycode) {

        Map<String, Object> response = new HashMap<>();

        // 检查订单是否已存在
        if (orderRepo.findByOrdernum(ordernum).isPresent()) {
            response.put("success", 1);
            response.put("message", "订单已接收");
            return response;
        }

        // 创建新的订单对象
        SwOrderInfo order = new SwOrderInfo();
        order.setAdid(adid);
        order.setAdname(adname);
        order.setAppid(appid);
        order.setOrdernum(ordernum);
        order.setDlevel(dlevel);
        order.setPagename(pagename);
        order.setAtype(atype);
        order.setDeviceid(deviceid);
        order.setSimid(simid);
        order.setAppsign(appsign);
        order.setMerid(merid);
        order.setEvent(event);
        order.setAdicon(adicon);
        order.setPrice(new BigDecimal(price));
        order.setMoney(new BigDecimal(money));
        order.setItime(LocalDateTime.parse(itime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));
        order.setKeycode(keycode);

        // 保存订单到数据库
        orderRepo.save(order);

        response.put("success", 1);
        response.put("message", "接收成功");
        return response;
    }
}
